package com.example.teachingSystem.controller;

import com.example.teachingSystem.pojo.form.user.AddUserForm;
import com.example.teachingSystem.pojo.form.user.ChangeUserInfoForm;
import com.example.teachingSystem.pojo.form.user.UserPageQuery;
import com.example.teachingSystem.pojo.form.user.UserRoleForm;
import com.example.teachingSystem.pojo.vo.UserInfoVo;
import com.example.teachingSystem.service.SysUserRoleService;
import com.example.teachingSystem.service.SysUserService;
import com.example.teachingSystem.utils.result.PageResult;
import com.example.teachingSystem.utils.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Tag(name = "2.用户模块")
public class UserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @PostMapping("/one")
    @Operation(summary = "添加用户（管理员）", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> addUser(@RequestBody AddUserForm addUserForm) {
        return sysUserService.addUser(addUserForm);
    }

    @PostMapping("/more")
    @Operation(summary = "批量添加用户（管理员）", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> addUsers(@RequestBody List<AddUserForm> addUserFormList) {
        return sysUserService.addUsers(addUserFormList);
    }

    @GetMapping("/me")
    @Operation(summary = "获取自己的用户信息", security = {@SecurityRequirement(name = "Authorization")})
    public Result<UserInfoVo> getMineInfo() {
        return sysUserService.getMineInfo();
    }

    @GetMapping("/other/{username}")
    @Operation(summary = "获取其他的用户信息", security = {@SecurityRequirement(name = "Authorization")})
    public Result<UserInfoVo> getOtherInfo(@PathVariable String username) {
        return sysUserService.getOtherInfo(username);
    }

    @GetMapping("/more")
    @Operation(summary = "用户分页（管理员）", security = {@SecurityRequirement(name = "Authorization")})
    public PageResult<UserInfoVo> getUserInfoList(UserPageQuery userPageQuery) {
        return sysUserService.getUserInfoList(userPageQuery);
    }

    @PutMapping("/one")
    @Operation(summary = "修改信息", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> updateUserInfo(@RequestBody ChangeUserInfoForm changeUserInfoForm) {
        return sysUserService.updateUserInfo(changeUserInfoForm);
    }

    @DeleteMapping("/disabled/{id}")
    @Operation(summary = "禁用用户（管理员）", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> disableUserStatus(@PathVariable Long id) {
        return sysUserService.changeUserStatus(id, true);
    }

    @PutMapping("/disabled/{id}")
    @Operation(summary = "取消禁用用户（管理员）", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> unDisableUserStatus(@PathVariable Long id) {
        return sysUserService.changeUserStatus(id, false);
    }

    @DeleteMapping("/deleted/{id}")
    @Operation(summary = "注销用户（管理员）", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> deleteUser(@PathVariable Long id) {
        return sysUserService.changeUserDeleted(id, true);
    }

    @PutMapping("/deleted/{id}")
    @Operation(summary = "恢复用户", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> recoverUser(@PathVariable Long id) {
        return sysUserService.changeUserDeleted(id, false);
    }

    @DeleteMapping("/me")
    @Operation(summary = "自己注销用户", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> deleteMine() {
        return sysUserService.deleteMine();
    }

    @PostMapping("/role")
    @Operation(summary = "为用户添加角色(管理员)", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> addUserRoles(@RequestBody UserRoleForm userRoleForm) {
        return sysUserRoleService.addUserRoles(userRoleForm);
    }

    @PutMapping("/role")
    @Operation(summary = "为用户删除角色(管理员)", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> deleteUserRole(@RequestBody UserRoleForm userRoleForm) {
        return sysUserRoleService.deleteUserRoles(userRoleForm);
    }

    @PutMapping("/reset/{id}")
    @Operation(summary = "重置密码(管理员)", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> deleteUserRole(@PathVariable Long id) {
        return sysUserService.resetUserPwd(id);
    }

    @PutMapping("/avatar")
    @Operation(summary = "修改头像", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> updateUserAvatar(String content) {
        return sysUserService.updateUserPartInfo(content, true, false);
    }

    @PutMapping("/sign")
    @Operation(summary = "修改标签", security = {@SecurityRequirement(name = "Authorization")})
    public Result<String> updateUserSign(String content) {
        return sysUserService.updateUserPartInfo(content, false, true
        );
    }

}